package com.chag.biz.userbase.service;

import com.chag.framework.common.ServiceResult;
import com.taobao.api.DefaultTaobaoClient;
import com.taobao.api.TaobaoClient;
import com.taobao.api.request.AlibabaAliqinFcSmsNumSendRequest;
import com.taobao.api.response.AlibabaAliqinFcSmsNumSendResponse;
import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.DateFormatUtils;
import org.apache.log4j.Logger;

import java.util.Date;

/**
 * Created with IntelliJ IDEA.
 * User: CHAG
 * Date: 2016/6/8
 * Time: 23:26
 * 短信发送实现接口.
 */
public class MessageServiceImpl implements IMessageService {
    private Logger logger = Logger.getLogger(getClass());

    /**
     * 短信发送方法
     *
     * @param phone        手机号码
     * @param validateCode 验证码
     * @return
     */
    @Override
    public ServiceResult<AlibabaAliqinFcSmsNumSendResponse> sendValidateCode(String phone, String validateCode) throws Exception {
        TaobaoClient client = new DefaultTaobaoClient("http://gw.api.taobao.com/router/rest", "23385458", "955da4ab48cf0e04cdab92ea2fda9848");
        AlibabaAliqinFcSmsNumSendRequest req = new AlibabaAliqinFcSmsNumSendRequest();
        req.setExtend("123456");
        req.setSmsType("normal");
        req.setSmsFreeSignName("阿里大鱼");
        req.setSmsParamString("{\"code\":\"1234\",\"product\":\"alidayu\"}");
        req.setRecNum(phone);
        req.setSmsTemplateCode("SMS_10655048");
        AlibabaAliqinFcSmsNumSendResponse rsp = client.execute(req);
        System.out.println(rsp.getBody());
        return new ServiceResult<AlibabaAliqinFcSmsNumSendResponse>(Boolean.TRUE, rsp, "短信发送成功！");
    }

    /**
     * 短信发送消息服务
     *
     * @param phone   手机号码
     * @param content 短信内容
     * @return
     */
    @Override
    public ServiceResult<String> sendMessage(String phone, String content) throws Exception {
        if (StringUtils.isBlank(phone)) {
            return new ServiceResult<String>(Boolean.FALSE, "手机号码不能为空！");
        }


        HttpClient client = new HttpClient();
        PostMethod post = new PostMethod("http://124.172.184.144:8888/sms.aspx");
        post.addRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");

        String dateStr = DateFormatUtils.format(new Date(), DateFormatUtils.ISO_DATETIME_FORMAT.getPattern());

        NameValuePair[] data = {new NameValuePair("userid", "2080"), new NameValuePair("account", "lingmengwang"), new NameValuePair("password", "sdh123456"), new NameValuePair("mobile", phone), new NameValuePair("content", content), new NameValuePair("sendTime", dateStr), new NameValuePair("action", "send"), new NameValuePair("extno", "")};

        post.setRequestBody(data);
        client.executeMethod(post);
        Header[] headers = post.getResponseHeaders();
        int statusCode = post.getStatusCode();
        System.out.println("statusCode:" + statusCode);
        logger.info("statusCode:" + statusCode);
        for (Header h : headers) {
            logger.info(h.toString());
        }

        String result = new String(post.getResponseBodyAsString().getBytes());
        logger.info(result);    //打印返回消息状态
        post.releaseConnection();

        return new ServiceResult<String>(Boolean.TRUE, result, "短信发送成功！");
    }
}
